<template>
  <div class="container">
    <div class="box">
        <div class="title">
          <img :src="logo">
          <h2>物流智能管理系统</h2>
        </div>
        <div class="form">
          <el-form :model="ruleForm" :rules="rules" ref="ruleForm"  class="demo-ruleForm">
            <el-form-item  prop="username">
              <el-input v-model="ruleForm.username"></el-input>
            </el-form-item>
            <el-form-item  prop="pwd">
              <el-input v-model="ruleForm.pwd"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="warning" @click="submitForm('ruleForm')">登录</el-button>
            </el-form-item>
          </el-form>
        </div>
    </div>
  </div>
</template>

<script>
import logo from '@s/logo.png'
import {loginApi} from '@a/loginApi.js'
import {setToken} from '../utils/cookieUtils'
export default {
    data() {
      return {
        logo:logo,
        ruleForm: {
          username: '',
          pwd: ''
        },
        rules: {
          username: [
            { required: true, message: '请输入用户名', trigger: 'blur' },
            { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
          ],
          pwd: [
            { required: true, message: '请输入密码', trigger: 'blur' },
          ]
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            console.log(this.ruleForm);
            loginApi(this.ruleForm).then(res=>{
              if(res.code==200){
                setToken('token',res.data);
                this.$router.push('/')
              }else{
                 this.$message({
                    message: res.msg,
                    type: 'warning'
                 });
              }
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      }
    }
}
</script>

<style lang="less" scoped>
  .container{
    width:100%;
    height: 100%;
    background: url('../assets/bg9.jpg') no-repeat center center;
    background-size: 100% 100%;
    .box{
      width: 350px;
      height: 280px;
      background: #fff;
      border-radius: 6px;
      padding:0 20px;
      position: absolute;
      left: 50%;
      top:50%;
      transform: translate(-50%,-50%);
      .title{
        height: 60px;
        display: flex;
        justify-content: center;
        align-items: center;
        img{
          width: 30px;
          height: 30px;
          margin-right:15px
        }
        h2{
          font-size: 20px;
        }
      };
      .el-button{
        width: 100%;
      }
    }
  }
</style>